package com.yc.forum.dao;

import com.yc.forum.util.DBHelper;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program:Knowledge_Forum
 * @description:
 * @author:YYF
 * @create:2022-05-16 19:09
 */
public class ProblemDao {
    //根据问题id获取一条问题的详细详细
    public Map<String,Object> selectOneProblemById(String problemid){
        String sql="select * from problem where id=?";
        return DBHelper.selectOneMap(sql,problemid);
    }
    //发布问题
    public int addProblem(String account,String title,String module,int reward,String body){
        String sql="insert into problem(account,module,reward,title,body) value(?,?,?,?,?)";
        return DBHelper.update(sql,account,module,reward,title,body);
    }
    //分模块 分页查询 问题
    public Map<String,Object> selectClassify(String module, String page, String size,String search){
        String sql;
//        String sql3;
        if(search==null||search.isEmpty()) {
            sql = "select * from problem where module = ? order by begintime DESC";
//            sql3= "select body from problem where module = ? order by begintime DESC";
        }else{
            sql="select * from problem where title like ? or body like ? order by begintime DESC";
//            sql3= "select body from problem where title like ? or body like ? order by begintime DESC";
            search="%"+search+"%";
        }
        int start=(Integer.parseInt(page)-1)*Integer.parseInt(size);
        String pagesql=sql+" limit "+start+","+size+"";
        List<Map<String,Object>> list;
        long total;
//        List<Map<String,Object>> newbody;
        if(search==null||search.isEmpty()) {
            list= DBHelper.selectListMap(pagesql, module);
            total=DBHelper.count(sql,module);
//            newbody=DBHelper.selectListMap(sql3,module);
        }else{
            list = DBHelper.selectListMap(pagesql, search,search);
            total=DBHelper.count(sql,search,search);
//            newbody=DBHelper.selectListMap(sql3,search,search);
        }
        Map<String,Object> map=new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            //改变时间字段
            map=list.get(i);
            String time=map.get("begintime").toString().substring(0,10);
            map.put("begintime",time);
            //取出body
        }

//        System.out.println("newbody多合一 "+newbody.toString());
//        for (int i = 0; i < newbody.size(); i++) {
//            System.out.println("newbody  正文："+newbody.get(i));
//        }

        Map<String,Object> pageData=new HashMap<>();
//        pageData.put("newbody",newbody);
        pageData.put("total",total);
        pageData.put("list",list);
        return pageData;
    }
    //查询热门问题
    public List<Map<String,Object>> selectHotProblem(){
        //取出点赞最高的10个问题的id-->size
        final int size=10;
        //取出10个最热问题主键
        String sql1="select DISTINCT problemkey from post  where problemkey is not NULL GROUP BY problemkey ORDER BY  COUNT( problemkey) DESC LIMIT ?";
        List<Map<String,Object>> ids =DBHelper.selectListMap(sql1,size);

        List<Object> params=new ArrayList<>();
        for (int i = 0; i < size; i++) {
            String id;
            try {
                id=""+ids.get(i).get("problemkey");
            }catch (IndexOutOfBoundsException e){
                id=null;
            }
            System.out.println("id======="+id);
            if(id==null || id.trim().isEmpty() || id.equals("null")){
                params.add("0");
            }else {
                params.add(id);
            }
        }
        //查出问题
        String sql2="select * from problem where id in (?,?,?,?,?,?,?,?,?,?) ";
        List<Map<String,Object>> list2= DBHelper.selectListMap(sql2, params.toArray());
        return list2;
    }
}
